package cn.cvs.service.sysRole;

import cn.cvs.dao.SysRoleMapper;
import cn.cvs.dao.SysUserMapper;
import cn.cvs.entity.SysRole;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 角色模块业务层实现
 */
@Service
public class SysRoleServiceImpl implements SysRoleService{
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Override
    public List<SysRole> getRoleList() throws Exception{
        // TODO Auto-generated method stub
        return sysRoleMapper.selectList();
    }

    @Override
    public boolean add(SysRole sysRole) throws Exception {
        // TODO Auto-generated method stub
        boolean flag = false;
        sysRole.setCreatedTime(new Date());
        if(sysRoleMapper.add(sysRole) > 0)
            flag = true;
        return flag;
    }

    @Override
    public boolean deleteById(Integer id) throws Exception {
        // TODO Auto-generated method stub
        boolean flag = false;
        if(sysUserMapper.selectCount(null, id) == 0){
            if(sysRoleMapper.deleteById(id) > 0)
                flag = true;
        }
        return flag;
    }

    @Override
    public boolean update(SysRole sysRole) throws Exception {
        // TODO Auto-generated method stub
        boolean flag = false;
        sysRole.setUpdatedTime(new Date());
        if(sysRoleMapper.update(sysRole) > 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public SysRole getById(Integer id) throws Exception {
        // TODO Auto-generated method stub
        return sysRoleMapper.selectById(id);
    }

    @Override
    public int getCountByCode(String code) throws Exception {
        // TODO Auto-generated method stub
        return sysRoleMapper.selectByCode(code);
    }

}
